package com.online.music.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.online.music.entity.po.Song;
import com.online.music.entity.po.Type;
import com.online.music.mapper.SongMapper;
import com.online.music.mapper.TypeMapper;
import com.online.music.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;

import com.online.music.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: TypeServiceImpl
 */
@Service
@RequiredArgsConstructor
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    private final TypeMapper typeMapper;

    private final SongMapper songMapper;

    @Override
    public Result<Page<Type>> findPage(Integer pageNum, Integer pageSize, Type type) {
        Page<Type> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
        // 添加条件...
        if (StrUtil.isNotBlank(type.getName())) {
            lqw.like(Type::getName, type.getName());
        }
        lqw.orderByDesc(Type::getSort);
        return Result.success(page(page, lqw));
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        // 删除前先判断是否存在...
        // 判断是否有歌曲关联
        Long count = songMapper.selectCount(
                new LambdaQueryWrapper<Song>().in(Song::getTypeId, ids)
        );
        if (count > 0) {
            return Result.error("删除失败，该类型下有歌曲关联");
        }
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

}

